import org.junit.Test
import java.util.*
import kotlin.test.assertEquals

/**
 * Fucked by LPJ on 2018/1/22.
 */
class `strings-mixTest` {
    // ---------------------------
    @Test
    fun test() {
        println("Fixed Tests")
        assertEquals("2:eeeee/2:yy/=:hh/=:rr", mix("Are they here", "yes, they are here"))
        assertEquals("1:ooo/1:uuu/2:sss/=:nnn/1:ii/2:aa/2:dd/2:ee/=:gg",
                mix("looping is fun but dangerous", "less dangerous than coding"))
        assertEquals("1:aaa/1:nnn/1:gg/2:ee/2:ff/2:ii/2:oo/2:rr/2:ss/2:tt",
                mix(" In many languages", " there's a pair of functions"))
        assertEquals("1:ee/1:ll/1:oo", mix("Lords of the Fallen", "gamekult"))
        assertEquals("", mix("codewars", "codewars"))
        assertEquals("1:nnnnn/1:ooooo/1:tttt/1:eee/1:gg/1:ii/1:mm/=:rr",
                mix("A generation must confront the looming ", "codewarrs"))
    }
    //.......................
    @Test
    fun test1() {
        println("100 Random Tests")
        for (i in 0..9)
        {
            val s1 = doEx()
            val s2 = doEx()
            println(s1)
            println(s2)
            println(mixSol(s1, s2))
            println("****")
            assertEquals(mixSol(s1, s2), mix(s1, s2))
        }
    }
    companion object {
        private fun randInt(min:Int, max:Int):Int {
            return min + (Math.random() * ((max - min) + 1)).toInt()
        }
        fun doEx():String {
            var res = ""
            var n = -1
            for (i in 0..29)
            {
                if (i % 5 == 0)
                    n = randInt(65, 90)
                else
                    n = randInt(97, 122)
                res += n.toChar()
            }
            return res
        }
        // ---------------------------
        fun mixSol(s1:String, s2:String):String {
            val alpha1 = IntArray(26)
            for (i in alpha1.indices) alpha1[i] = 0
            val alpha2 = IntArray(26)
            for (i in alpha2.indices) alpha2[i] = 0
            for (i in 0 until s1.length)
            {
                val c = s1.get(i).toInt()
                if (c >= 97 && c <= 122)
                    alpha1[c - 97]++
            }
            for (i in 0 until s2.length)
            {
                val c = s2.get(i).toInt()
                if (c >= 97 && c <= 122)
                    alpha2[c - 97]++
            }
            var res = ""
            for (i in 0..25)
            {
                var sm = Math.max(alpha1[i], alpha2[i])
                if (sm > 1)
                {
                    if (sm > alpha1[i])
                    {
                        res += "2:"
                    }
                    else
                    {
                        if (sm > alpha2[i])
                        {
                            res += "1:"
                        }
                        else
                        {
                            res += "=:"
                        }
                    }
                    for (j in 0 until sm)
                    {
                        res += (i + 97).toChar()
                    }
                    res += "/"
                }
            }
            val cmp = object:Comparator<String> {
                public override fun compare(o1:String, o2:String):Int {
                    val cp = o1.length - o2.length
                    if (cp == 0)
                        return o1.compareTo(o2)
                    if (cp > 0) return -1 else return 1
                }
            }
            if (res.length == 0) return ""
            val lstr = res.substring(0, res.length - 1).split(("/").toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()
            Arrays.sort<String>(lstr, cmp)
            res = lstr.joinToString("/")
            return res
        }
    }
}